package com.richman.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import com.richman.controller.NegativeRatioClient;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.ISNEvent;
import com.richman.ps38.entity.ISNPS38Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.view.HGPS38EventPane.MyTableModel;

public class HGPS38EventPane extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -88L;
	
	private  Vector<String[]> detailsData = new Vector<String[]>();
	
	//public Vector<Event> gameDetailsVec = new Vector<Event>(); 
	
	private int focusedRowIndex = -1;
	
	static boolean bet = false;
	
	private JPopupMenu m_popupMenu;
	private JMenuItem betWholeDQMenItem; 
	private JMenuItem betWholeXQMenItem; 
	
	private JMenuItem betHalfDXQMenItem;
	
	private JMenuItem betWholeRQPHomeMenItem;
	private JMenuItem betWholeRQPAwayMenItem;
	
	private JMenuItem betHalfRQPHomeMenItem;
	private JMenuItem betHalfRQPAwayMenItem;
	
	private JMenuItem upTeamMenItem;
	
	//private JMenuItem betHalfMenItem;
	
	private String currentbetstr = "";

	private Vector<String> upTeams = new Vector<String>();
	
	MyTableModel tableMode = new MyTableModel();

	Vector<SendData> sendDatas = new Vector<SendData>();
	
	JTable table = null;

	/**
	 * Create the panel.
	 */
	public HGPS38EventPane() {
		setLayout(new BorderLayout(0, 0));

		
	    table = new JTable(tableMode);
	    
	    table.getColumnModel().getColumn(0).setPreferredWidth(120);
	    
	    table.setRowHeight(30);
	    
	    table.setFont(new java.awt.Font("黑体", Font.PLAIN, 15));
		
        table.addMouseListener(new java.awt.event.MouseAdapter() {  
            public void mouseClicked(java.awt.event.MouseEvent evt) {  
                jTable1MouseClicked(evt);  
            }  
        });  
        
        createPopupMenu();
	    
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		


	}
	
	
	private void createPopupMenu() {
		

		SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
        m_popupMenu = new JPopupMenu();            
        betWholeDQMenItem = new JMenuItem();  
        betWholeDQMenItem.setText("投注全场大");  
        betWholeDQMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < detailsData.size()){
	            		String key = detailsData.elementAt(focusedRowIndex)[3] + detailsData.elementAt(focusedRowIndex)[8];
	            		
	            		SendData tmp=null;
	            		for(int i =0; i < sendDatas.size(); i++) {
	            			if((sendDatas.elementAt(i).isnps38event.homeTeam +"-vs-" + sendDatas.elementAt(i).isnps38event.awayTeam + sendDatas.elementAt(i).isnps38event.wholeDXPPankou).equals(key)) {
	            				tmp = sendDatas.elementAt(i);
	            				break;
	            				
	            			}  
	            		}
	            		
	            		if(tmp==null) {
	            			return;
	            		}
	            		
	            		String betodd = Double.toString(NegativeRatioClient.mainWnd.betodd);
	            		int betamount = NegativeRatioClient.mainWnd.betAmount;
	            		

	        			//String betstr = leaguename + teams + time + pankou + score +  "全场大小";
	        			
	        			
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.wholeDXPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr_first = leaguename + teams + time  + "全场大球|" + pankou + "|"  + score;
	        			String betID = Long.toString(System.currentTimeMillis());
	        			String betstr = betstr_first +   "|" + betodd + "|" + Integer.toString(betamount)+ "|" + betID;
	        			
	        			
	        			int n = JOptionPane.showConfirmDialog(null, "投注全场大球 " + tmp.isnps38event.homeTeam + "  " + pankou, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(NegativeRatioClient.betThread.betVec.contains(betstr_first)) {
	    						JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + " 该投注已存在投注队列中,请先取消之前的投注", "错误",
	    	    						JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
	    						return;
	    					}
	    					if(!tmp.isnps38event.wholeDXPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr );
		            			
		            			BetAdyData bad = new BetAdyData();
		            			
		            			bad.gid = betID;
		            			bad.currenttime = bad.currenttime = dfSec.format(System.currentTimeMillis());
		            			bad.league = tmp.isnps38event.leagueName;
		            			bad.time = tmp.isnps38event.eventTime;
		            			bad.teams  = tmp.isnps38event.homeTeam + "-vs-"  +tmp.isnps38event.awayTeam;
		            			bad.bettype = "大";
		            			bad.betpankou = pankou;

		            			bad.ps38bettype = "大|最低水位:"+ betodd + "|投注金额:" + betamount;
		            			bad.ps38betgold = "0";
								bad.ps38betres = "100";
								bad.score = score;
								bad.websitetype = "betCode " + betID;
								bad.betAmount = Integer.toString(betamount);
								
								NegativeRatioClient.betCtrMng.updateBetLogData(bad);
		            			
		            			JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + " 大   已加入投注队列,开始投注 ", "确认",
	    	    						JOptionPane.YES_OPTION,JOptionPane.CLOSED_OPTION);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	        			
	        			
	        			
	        			
	        			
	        			
	        			
//	        			int n = JOptionPane.showConfirmDialog(null, "投注 " + teams + " 全场大", "投注确认",
//	    						JOptionPane.YES_NO_OPTION);
//	    				if (n == JOptionPane.YES_OPTION) {
//	    					if(!tmp.isnps38event.wholeDXPPankou.equals("")) {
//		            			NegativeRatioClient.betThread.betVec.add(betstr);
//		            		}
//	    				}else{
//	    					System.out.println("取消投注");
//	    					
//	    				}
	        			
	            		
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });
        

        
        betWholeXQMenItem = new JMenuItem();  
        betWholeXQMenItem.setText("投注全场小");  
        betWholeXQMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < detailsData.size()){
	            		String key = detailsData.elementAt(focusedRowIndex)[3] + detailsData.elementAt(focusedRowIndex)[8];
	            		
	            		SendData tmp=null;
	            		for(int i =0; i < sendDatas.size(); i++) {
	            			if((sendDatas.elementAt(i).isnps38event.homeTeam +"-vs-" + sendDatas.elementAt(i).isnps38event.awayTeam + sendDatas.elementAt(i).isnps38event.wholeDXPPankou).equals(key)) {
	            				tmp = sendDatas.elementAt(i);
	            				break;
	            				
	            			}  
	            		}
	            		
	            		if(tmp==null) {
	            			return;
	            		}
	            		
	            		String betodd = Double.toString(NegativeRatioClient.mainWnd.betodd);
	            		int betamount = NegativeRatioClient.mainWnd.betAmount;
	            		

	        			//String betstr = leaguename + teams + time + pankou + score +  "全场大小";
	        			
	        			
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.wholeDXPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr_first = leaguename + teams + time  + "全场小球|" + pankou + "|"  + score;
	        			String betID = Long.toString(System.currentTimeMillis());
	        			String betstr = betstr_first +   "|" + betodd + "|" + Integer.toString(betamount)+ "|" + betID;
	        			
	        			
	        			int n = JOptionPane.showConfirmDialog(null, "投注全场小球 " + tmp.isnps38event.homeTeam + "  " + pankou, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(NegativeRatioClient.betThread.betVec.contains(betstr_first)) {
	    						JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + " 该投注已存在投注队列中,请先取消之前的投注", "错误",
	    	    						JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
	    						return;
	    					}
	    					if(!tmp.isnps38event.wholeDXPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr );
		            			
		            			BetAdyData bad = new BetAdyData();
		            			
		            			bad.gid = betID;
		            			bad.currenttime = bad.currenttime = dfSec.format(System.currentTimeMillis());
		            			bad.league = tmp.isnps38event.leagueName;
		            			bad.time = tmp.isnps38event.eventTime;
		            			bad.teams  = tmp.isnps38event.homeTeam + "-vs-"  +tmp.isnps38event.awayTeam;
		            			bad.bettype = "小";
		            			bad.betpankou = pankou;

		            			bad.ps38bettype = "小|最低水位:"+ betodd + "|投注金额:" + betamount;
		            			bad.ps38betgold = "0";
								bad.ps38betres = "100";
								bad.score = score;
								bad.websitetype = "betCode " + betID;
								bad.betAmount = Integer.toString(betamount);
								
								NegativeRatioClient.betCtrMng.updateBetLogData(bad);
		            			
		            			JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + "  小  已加入投注队列,开始投注 ", "确认",
	    	    						JOptionPane.YES_OPTION,JOptionPane.CLOSED_OPTION);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	        			
	        			
	        			
	        			
	        			
	        			
	        			
//	        			int n = JOptionPane.showConfirmDialog(null, "投注 " + teams + " 全场大", "投注确认",
//	    						JOptionPane.YES_NO_OPTION);
//	    				if (n == JOptionPane.YES_OPTION) {
//	    					if(!tmp.isnps38event.wholeDXPPankou.equals("")) {
//		            			NegativeRatioClient.betThread.betVec.add(betstr);
//		            		}
//	    				}else{
//	    					System.out.println("取消投注");
//	    					
//	    				}
	        			
	            		
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });
        
        
        betWholeRQPHomeMenItem = new JMenuItem();
        betWholeRQPHomeMenItem.setText("投注全场主队");
        betWholeRQPHomeMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < detailsData.size()){
	            		
	            		String key = detailsData.elementAt(focusedRowIndex)[3] + detailsData.elementAt(focusedRowIndex)[4];
	            		
	            		SendData tmp=null;
	            		for(int i =0; i < sendDatas.size(); i++) {
	            			if((sendDatas.elementAt(i).isnps38event.homeTeam +"-vs-" + sendDatas.elementAt(i).isnps38event.awayTeam + sendDatas.elementAt(i).isnps38event.wholeRQPPankou).equals(key)) {
	            				tmp = sendDatas.elementAt(i);
	            				break;
	            				
	            			}  
	            		}
	            		
	            		if(tmp==null) {
	            			return;
	            		}
	            		
	            		String betodd = Double.toString(NegativeRatioClient.mainWnd.betodd);
	            		int betamount = NegativeRatioClient.mainWnd.betAmount;
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.wholeRQPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr_first = leaguename + teams + time  + "全场让球主|" + pankou + "|"  + score;
	        			String betID = Long.toString(System.currentTimeMillis());
	        			String betstr = betstr_first +   "|" + betodd + "|" + Integer.toString(betamount)+ "|" + betID;
	        			int n = JOptionPane.showConfirmDialog(null, "投注全场让球 " + tmp.isnps38event.homeTeam + "  " + pankou, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(NegativeRatioClient.betThread.betVec.contains(betstr_first)) {
	    						JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + " 该投注已存在投注队列中,请先取消之前的投注", "错误",
	    	    						JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
	    						return;
	    					}
	    					if(!tmp.isnps38event.wholeRQPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr );
		            			
		            			BetAdyData bad = new BetAdyData();
		            			
		            			bad.gid = betID;
		            			bad.currenttime = bad.currenttime = dfSec.format(System.currentTimeMillis());
		            			bad.league = tmp.isnps38event.leagueName;
		            			bad.time = tmp.isnps38event.eventTime;
		            			bad.teams  = tmp.isnps38event.homeTeam + "-vs-"  +tmp.isnps38event.awayTeam;
		            			bad.bettype = "主";
		            			bad.betpankou = pankou;

		            			bad.ps38bettype = tmp.isnps38event.homeTeam + "|最低水位:"+ betodd + "|投注金额:" + betamount;
		            			bad.ps38betgold = "0";
								bad.ps38betres = "100";
								bad.score = score;
								bad.websitetype = "betCode " + betID;
								bad.betAmount = Integer.toString(betamount);
								
								NegativeRatioClient.betCtrMng.updateBetLogData(bad);
		            			
		            			JOptionPane.showConfirmDialog(null, tmp.isnps38event.homeTeam + " 已加入投注队列,开始投注 ", "确认",
	    	    						JOptionPane.YES_OPTION,JOptionPane.CLOSED_OPTION);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        betWholeRQPAwayMenItem = new JMenuItem();
        betWholeRQPAwayMenItem.setText("投注全场客队");
        betWholeRQPAwayMenItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < detailsData.size()){
	            		String key = detailsData.elementAt(focusedRowIndex)[3] + detailsData.elementAt(focusedRowIndex)[4];
	            		
	            		SendData tmp=null;
	            		for(int i =0; i < sendDatas.size(); i++) {
	            			if((sendDatas.elementAt(i).isnps38event.homeTeam +"-vs-" + sendDatas.elementAt(i).isnps38event.awayTeam + sendDatas.elementAt(i).isnps38event.wholeRQPPankou).equals(key)) {
	            				tmp = sendDatas.elementAt(i);
	            				break;
	            				
	            			}  
	            		}
	            		
	            		if(tmp==null) {
	            			return;
	            		}
	            		
	            		String betodd = Double.toString(NegativeRatioClient.mainWnd.betodd);
	            		int betamount = NegativeRatioClient.mainWnd.betAmount;
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.wholeRQPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr_first = leaguename + teams + time  + "全场让球客|" + pankou + "|"  + score;
	        			String betID = Long.toString(System.currentTimeMillis());
	        			String betstr = betstr_first +   "|" + betodd + "|" + Integer.toString(betamount) + "|" + System.currentTimeMillis() + "|" + betID;
	        			int n = JOptionPane.showConfirmDialog(null, "投注全场让球 " + tmp.isnps38event.awayTeam, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {
	    					if(NegativeRatioClient.betThread.betVec.contains(betstr_first)) {
	    						JOptionPane.showConfirmDialog(null, tmp.isnps38event.awayTeam + " 该投注已在投注队列中,请等待投注完成 ", "错误",
	    	    						JOptionPane.YES_OPTION,JOptionPane.ERROR_MESSAGE);
	    						return;
	    					}
	    					if(!tmp.isnps38event.wholeRQPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr);
		            			
		            			BetAdyData bad = new BetAdyData();
		            			
		            			bad.gid = betID;
		            			bad.currenttime = bad.currenttime = dfSec.format(System.currentTimeMillis());
		            			bad.league = tmp.isnps38event.leagueName;
		            			bad.time = tmp.isnps38event.eventTime;
		            			bad.teams  = tmp.isnps38event.homeTeam + "-vs-"  +tmp.isnps38event.awayTeam;
		            			bad.bettype = "客";
		            			bad.betpankou = pankou;

		            			bad.ps38bettype = tmp.isnps38event.awayTeam + "|最低水位:"+ betodd + "|投注金额:" + betamount;
		            			bad.ps38betgold = "0";
								bad.ps38betres = "100";
								bad.score = score;
								bad.websitetype = "betCode "+ betID;
								
								bad.betAmount = Integer.toString(betamount);
								
								NegativeRatioClient.betCtrMng.updateBetLogData(bad);
		            			
		            			JOptionPane.showConfirmDialog(null, tmp.isnps38event.awayTeam + " 已加入投注队列,开始投注 ", "确认",
	    	    						JOptionPane.YES_OPTION,JOptionPane.CLOSED_OPTION);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        
        
        
        

        
        
        betHalfRQPHomeMenItem = new JMenuItem();
        betHalfRQPHomeMenItem.setText("投注半场主队");
        betHalfRQPHomeMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < sendDatas.size()){
	            		SendData tmp = sendDatas.elementAt(focusedRowIndex);
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.halfRQPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr = leaguename + teams + time + pankou + score +  "半场让球主";
	        			int n = JOptionPane.showConfirmDialog(null, "投注半场让球 " + tmp.isnps38event.homeTeam, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(!tmp.isnps38event.halfRQPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr);
		            		}
	    				}else{
	    					System.out.println("取消投注");	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        
        
        betHalfRQPAwayMenItem = new JMenuItem();
        betHalfRQPAwayMenItem.setText("投注半场客队");
        betHalfRQPAwayMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < sendDatas.size()){
	            		SendData tmp = sendDatas.elementAt(focusedRowIndex);
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.halfRQPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr = leaguename + teams + time + pankou + score +  "半场让球客";
	        			int n = JOptionPane.showConfirmDialog(null, "投注半场让球 " + tmp.isnps38event.awayTeam, "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {					
	    					if(!tmp.isnps38event.halfRQPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr);
		            		}
	    				}else{
	    					System.out.println("取消投注");	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        betHalfDXQMenItem = new JMenuItem();
        betHalfDXQMenItem.setText("投注半场大");
        betHalfDXQMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
	            	if(focusedRowIndex != -1 && focusedRowIndex < sendDatas.size()){
	            		SendData tmp = sendDatas.elementAt(focusedRowIndex);
	            		
	        			String leaguename = tmp.isnps38event.leagueName;
	        			String teams = tmp.isnps38event.homeTeam + "-vs-" + tmp.isnps38event.awayTeam;
	        			String time = tmp.isnps38event.eventTime;
	        			String pankou = tmp.isnps38event.halfDXPPankou;
	        			String score = tmp.isnps38event.homeTeamScore + ":" + tmp.isnps38event.awayTeamScore;
	        			String betstr = leaguename + teams + time + pankou + score +  "半场大小";
	        			int n = JOptionPane.showConfirmDialog(null, "投注 " + teams + " 半场大", "投注确认",
	    						JOptionPane.YES_NO_OPTION);
	    				if (n == JOptionPane.YES_OPTION) {
	    					if(!tmp.isnps38event.halfDXPPankou.equals("")) {
		            			NegativeRatioClient.betThread.betVec.add(betstr);
		            		}
	    				}else{
	    					System.out.println("取消投注");
	    					
	    				}
	            		
	            	}
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });  
        
        
        upTeamMenItem = new JMenuItem();
        upTeamMenItem.setText("置顶比赛");
        upTeamMenItem.addActionListener(new java.awt.event.ActionListener() {  
            public void actionPerformed(java.awt.event.ActionEvent evt) {  
                //该操作需要做的事              	
            	try{
            		if(focusedRowIndex != -1 && focusedRowIndex < detailsData.size()){
            			Iterator<String> it = upTeams.iterator();
            	        while (it.hasNext()) {
            	            if (it.next().equals(detailsData.elementAt(focusedRowIndex)[3])) {
            	                it.remove();
            	            }
            	        }
            			upTeams.add(0,detailsData.elementAt(focusedRowIndex)[3]);
            		}
	            	
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }  
        });
        
        
//        m_popupMenu.add(betWholeDXQMenItem);
//        m_popupMenu.add(betHalfDXQMenItem);
        m_popupMenu.add(upTeamMenItem);
        m_popupMenu.add(betWholeRQPHomeMenItem);
        m_popupMenu.add(betWholeRQPAwayMenItem);
        m_popupMenu.add(betWholeDQMenItem);
        m_popupMenu.add(betWholeXQMenItem);
//        m_popupMenu.add(betHalfRQPHomeMenItem);
//        m_popupMenu.add(betHalfRQPAwayMenItem);
    }
	
	
	   private void mouseRightButtonClick(java.awt.event.MouseEvent evt) {  
	       //判断是否为鼠标的BUTTON3按钮，BUTTON3为鼠标右键  
	       if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {  
	           //通过点击位置找到点击为表格中的行  
	           focusedRowIndex = table.rowAtPoint(evt.getPoint());  
	           if (focusedRowIndex == -1) {  
	               return;  
	           }  
	           //将表格所选项设为当前右键点击的行  
	           table.setRowSelectionInterval(focusedRowIndex, focusedRowIndex);  
	           
	           SendData tmp = sendDatas.elementAt(focusedRowIndex);

   				String teams = tmp.isnevent.homeTeam + "-vs-" + tmp.isnevent.awayTeam;
 				
   				currentbetstr = teams;
	           
	           m_popupMenu.show(table, evt.getX(), evt.getY());  
	       }  
	  
	   } 
	   
		private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {  
			  
		       mouseRightButtonClick(evt);  
		}  
	
		
		private String showRatio(String ratio) {
			if(Common.isNum(ratio)) {
				
				if(Double.parseDouble(ratio) > 1.0) {
					return String.format("%.3f", (Double.parseDouble(ratio) - 2.0));
				}
				
			}
			
			return ratio;
		}
		
	public String findMax(String ps38UpStr, String hgUpStr, String isnUpStr) {
		// 计算调整后的值（负数时加2.0）
		try {
			double ps38Up = Common.isNum(ps38UpStr)?Double.parseDouble(ps38UpStr):0.0;
			double hgUp = Common.isNum(hgUpStr)?Double.parseDouble(hgUpStr):0.0;
			double isnUp = Common.isNum(isnUpStr)?Double.parseDouble(isnUpStr):0.0;
			double adjustedPs38Up = ps38Up < 0 ? ps38Up + 2.0 : ps38Up;
			double adjustedHgUp = hgUp < 0 ? hgUp + 2.0 : hgUp;
			double adjustedIsnUp = isnUp < 0 ? isnUp + 2.0 : isnUp;
			
			double maxUp = ps38Up;
			if (adjustedHgUp > (maxUp < 0 ? maxUp + 2.0 : maxUp)) {
			    maxUp = hgUp;
			}
			if (adjustedIsnUp > (maxUp < 0 ? maxUp + 2.0 : maxUp)) {
			    maxUp = isnUp;
			}
			if(maxUp!=0.0) {
				return Double.toString(maxUp);
			}					
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "0.0";
	}	
	
	public String findMin(String ps38DownStr, String hgDownStr, String isnDownStr) {
		try {
			double ps38Down = Common.isNum(ps38DownStr)?Double.parseDouble(ps38DownStr):0.0;
			double hgDown = Common.isNum(hgDownStr)?Double.parseDouble(hgDownStr):0.0;
			double isnDown = Common.isNum(isnDownStr)?Double.parseDouble(isnDownStr):0.0;

			double adjustedPs38Down = ps38Down < 0 ? ps38Down + 2.0 : ps38Down;
			double adjustedHgDown = hgDown < 0 ? hgDown + 2.0 : hgDown;
			double adjustedIsnDown = isnDown < 0 ? isnDown + 2.0 : isnDown;
			
			// 找出最小的down（比较调整后的值，但保留原始值）
			double minDown = ps38Down;
			if (adjustedHgDown < (minDown < 0 ? minDown + 2.0 : minDown)) {
			    minDown = hgDown;
			}
			if (adjustedIsnDown < (minDown < 0 ? minDown + 2.0 : minDown)) {
			    minDown = isnDown;
			}
			if(minDown!=0.0) {
				return Double.toString(minDown);
			}					
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "0.0";
	}
	
	
	public  void sortByUpTeams(Vector<String[]> detailsData, Vector<String> upTeams) {
	    detailsData.sort(new Comparator<String[]>() {
	        @Override
	        public int compare(String[] o1, String[] o2) {
	            String team1 = o1[3];
	            String team2 = o2[3];

	            // 获取球队在优先级列表中的索引
	            int index1 = upTeams.indexOf(team1);
	            int index2 = upTeams.indexOf(team2);

	            // 判断是否存在优先级列表中
	            boolean exists1 = index1 != -1;
	            boolean exists2 = index2 != -1;

	            // 情况1：两者都在优先级列表
	            if (exists1 && exists2) {
	                // 按优先级列表顺序排序
	                if (index1 != index2) {
	                    return Integer.compare(index1, index2);
	                }
	                // 同一优先级则保持原始顺序
	                return Integer.compare(Integer.parseInt(o1[0]), Integer.parseInt(o2[0]));
	            }

	            // 情况2：只有o1在优先级列表
	            if (exists1) {
	                return -1;
	            }

	            // 情况3：只有o2在优先级列表
	            if (exists2) {
	                return 1;
	            }

	            // 情况4：都不在优先级列表，保持原始顺序
	            return Integer.compare(Integer.parseInt(o1[0]), Integer.parseInt(o2[0]));
	        }
	    });
	}


		
	public void updateHGPS38EventTable(Vector<SendData> gamedetails){
		
		try{
			
			sortByEventTime(gamedetails);
			
			sendDatas = gamedetails;
			
			if(detailsData.size() != 0){
				detailsData.clear();
			}
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			for(int i = 0; i < gamedetails.size(); i++) {
				
				ISNPS38Event game = gamedetails.elementAt(i).isnps38event;
				Event ps38event = gamedetails.elementAt(i).event;
				ISNEvent isnevent = gamedetails.elementAt(i).isnevent;
				HGEvent hgevent = gamedetails.elementAt(i).hgevent;
				
				String hgPankouUp = showRatio(hgevent.wholeRQPPankouUp);
				String hgPankouDown = showRatio(hgevent.wholeRQPPankouDown);
				
				String ps38ratio = ps38event.wholeRQPPankouUp + "    " + ps38event.wholeRQPPankouDown;
				String hgratio = hgPankouUp + "    " + hgPankouDown;
				String isnratio = isnevent.wholeRQPPankouUp +"    " + isnevent.wholeRQPPankouDown;
				String ps38UpStr = ps38event.wholeRQPPankouUp,  hgUpStr = hgPankouUp,  isnUpStr = isnevent.wholeRQPPankouUp;
				String ps38DownStr = ps38event.wholeRQPPankouDown,  hgDownStr = hgPankouDown, isnDownStr = isnevent.wholeRQPPankouDown;
				if(Common.isNum(game.wholeRQPPankou)&&Double.parseDouble(game.wholeRQPPankou)>0) {
					String maxRatio = findMax(ps38event.wholeRQPPankouUp, hgPankouUp, isnevent.wholeRQPPankouUp);
					String minRatio = findMin(ps38event.wholeRQPPankouDown, hgPankouDown, isnevent.wholeRQPPankouDown);
					
					if(Common.isNum(ps38UpStr)&&Math.abs(Double.parseDouble(ps38UpStr)-Double.parseDouble(maxRatio))<0.0001) {
						ps38UpStr = "<span style='color:red'>" + ps38UpStr + "</span>";
					}
					if(Common.isNum(hgUpStr)&&Math.abs(Double.parseDouble(hgUpStr)-Double.parseDouble(maxRatio))<0.0001) {
						hgUpStr = "<span style='color:red'>" + hgUpStr + "</span>";
					}
					if(Common.isNum(isnUpStr)&&Math.abs(Double.parseDouble(isnUpStr)-Double.parseDouble(maxRatio))<0.0001) {
						isnUpStr = "<span style='color:red'>" + isnUpStr + "</span>";
					}
					//Min
					if(Common.isNum(ps38DownStr)&&Math.abs(Double.parseDouble(ps38DownStr)-Double.parseDouble(minRatio))<0.0001) {
						ps38DownStr = "<span style='color:green'>" + ps38DownStr + "</span>";
					}
					if(Common.isNum(hgDownStr)&&Math.abs(Double.parseDouble(hgDownStr)-Double.parseDouble(minRatio))<0.0001) {
						hgDownStr = "<span style='color:green'>" + hgDownStr + "</span>";
					}
					if(Common.isNum(isnDownStr)&&Math.abs(Double.parseDouble(isnDownStr)-Double.parseDouble(minRatio))<0.0001) {
						isnDownStr = "<span style='color:green'>" + isnDownStr + "</span>";
					}
					
				}else {
										
					String minRatio = findMin(ps38event.wholeRQPPankouUp, hgPankouUp, isnevent.wholeRQPPankouUp);
					String maxRatio = findMax(ps38event.wholeRQPPankouDown, hgPankouDown, isnevent.wholeRQPPankouDown);
					
					if(Common.isNum(ps38UpStr)&&Math.abs(Double.parseDouble(ps38UpStr)-Double.parseDouble(minRatio))<0.0001) {
						ps38UpStr = "<span style='color:green'>" + ps38UpStr + "</span>";
					}
					if(Common.isNum(hgUpStr)&&Math.abs(Double.parseDouble(hgUpStr)-Double.parseDouble(minRatio))<0.0001) {
						hgUpStr = "<span style='color:green'>" + hgUpStr + "</span>";
					}
					if(Common.isNum(isnUpStr)&&Math.abs(Double.parseDouble(isnUpStr)-Double.parseDouble(minRatio))<0.0001) {
						isnUpStr = "<span style='color:green'>" + isnUpStr + "</span>";
					}
					
					if(Common.isNum(ps38DownStr)&&Math.abs(Double.parseDouble(ps38DownStr)-Double.parseDouble(maxRatio))<0.0001) {
						ps38DownStr = "<span style='color:red'>" + ps38DownStr + "</span>";
					}
					if(Common.isNum(hgDownStr)&&Math.abs(Double.parseDouble(hgDownStr)-Double.parseDouble(maxRatio))<0.0001) {
						hgDownStr = "<span style='color:red'>" + hgDownStr + "</span>";
					}
					if(Common.isNum(isnDownStr)&&Math.abs(Double.parseDouble(isnDownStr)-Double.parseDouble(maxRatio))<0.0001) {
						isnDownStr = "<span style='color:red'>" + isnDownStr + "</span>";
					}
					
				}
				
				
				ps38ratio = "<html>" + ps38UpStr + "    " + ps38DownStr;
				hgratio = "<html>" + hgUpStr + "    " + hgDownStr;
				isnratio = "<html>" + isnUpStr +"    " + isnDownStr; 
				
				
				
				
				//大小
				String hgDXUp = showRatio(hgevent.wholeDXPPankouUp);
				String hgDXDown = showRatio(hgevent.wholeDXPPankouDown);
				
				String ps38ratio_dx = ps38event.wholeDXPPankouUp + "    " + ps38event.wholeDXPPankouDown;
				String hgratio_dx = hgDXUp + "    " + hgDXDown;
				String isnratio_dx = isnevent.wholeDXPPankouUp +"    " + isnevent.wholeDXPPankouDown;
				
				String ps38DQStr = ps38event.wholeDXPPankouUp,  hgDQStr = hgDXUp,  isnDQStr = isnevent.wholeDXPPankouUp;
				String ps38XQStr = ps38event.wholeDXPPankouDown,  hgXQStr = hgDXDown, isnXQStr = isnevent.wholeDXPPankouDown;
				if(true) {
					String maxRatio = findMax(ps38event.wholeDXPPankouUp, hgDXUp, isnevent.wholeDXPPankouUp);
					String minRatio = findMin(ps38event.wholeDXPPankouDown, hgDXDown, isnevent.wholeDXPPankouDown);
					
					if(Common.isNum(ps38DQStr)&&Math.abs(Double.parseDouble(ps38DQStr)-Double.parseDouble(maxRatio))<0.0001) {
						ps38DQStr = "<span style='color:red'>" + ps38DQStr + "</span>";
					}
					if(Common.isNum(hgDQStr)&&Math.abs(Double.parseDouble(hgDQStr)-Double.parseDouble(maxRatio))<0.0001) {
						hgDQStr = "<span style='color:red'>" + hgDQStr + "</span>";
					}
					if(Common.isNum(isnDQStr)&&Math.abs(Double.parseDouble(isnDQStr)-Double.parseDouble(maxRatio))<0.0001) {
						isnDQStr = "<span style='color:red'>" + isnDQStr + "</span>";
					}
					//Min
					if(Common.isNum(ps38XQStr)&&Math.abs(Double.parseDouble(ps38XQStr)-Double.parseDouble(minRatio))<0.0001) {
						ps38XQStr = "<span style='color:green'>" + ps38XQStr + "</span>";
					}
					if(Common.isNum(hgXQStr)&&Math.abs(Double.parseDouble(hgXQStr)-Double.parseDouble(minRatio))<0.0001) {
						hgXQStr = "<span style='color:green'>" + hgXQStr + "</span>";
					}
					if(Common.isNum(isnXQStr)&&Math.abs(Double.parseDouble(isnXQStr)-Double.parseDouble(minRatio))<0.0001) {
						isnXQStr = "<span style='color:green'>" + isnXQStr + "</span>";
					}
					
				}
				
				
				ps38ratio_dx = "<html>" + ps38DQStr + "    " + ps38XQStr;
				hgratio_dx = "<html>" + hgDQStr + "    " + hgXQStr;
				isnratio_dx = "<html>" + isnDQStr +"    " + isnXQStr; 
				

				String time = game.duringTime;
				if(time.equals("")) {
					time = game.eventTime;
				}
				//{ "序号", "联赛", "时间", "球队", "让球盘",  "大小盘",   "半场让球盘 ", "大小盘"};
				String[] item = {Integer.toString(i+1), game.leagueName,time, game.homeTeam + "-vs-" + game.awayTeam,									
									game.wholeRQPPankou, ps38ratio, isnratio,									
									hgratio,
									game.wholeDXPPankou, ps38ratio_dx,isnratio_dx,hgratio_dx,
									game.homeTeamScore + ":" + game.awayTeamScore};
				detailsData.add(item);
				
//				if(SpecialServer.betCtrMng.ps38manager.getStatuscode() == 2&&bet==true&&gamedetails.size()>0) {
//					SpecialServer.betCtrMng.autobetCtrMng.dobetgame("单式全场大小", "2", gamedetails.elementAt(0));
//					bet = true;
//				}
				
				//SpecialServer.betCtrMng.startToBet(gamedetails);
				
			}
			
			sortByUpTeams(detailsData,upTeams);

			setOneRowBackgroundColor();
			
			tableMode.updateTable();

			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	
	
    public void setOneRowBackgroundColor() {
    	

    	
        try {  
        	
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            	
            	
  
                public Component getTableCellRendererComponent(JTable table,  
                        Object value, boolean isSelected, boolean hasFocus,  
                        int row, int column) {  

                	
                    if (row%2==1) {  
                    	setBackground(new Color(246,246,246));  
                        
                    }else{  
                        setBackground(new Color(222,222,243));  
                    	//setBackground(new Color(1,1,1));  
                      
                    }  
                    
                    setHorizontalAlignment(JLabel.CENTER);
  
                    return super.getTableCellRendererComponent(table, value,  
                            isSelected, hasFocus, row, column);  
                }  
            };  
            int columnCount = table.getColumnCount();  
            for (int i = 0; i < columnCount; i++) {  
            	table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    
    }  
	
	
    public static void sortByEventTime(Vector<SendData> gamedetails) {
        Collections.sort(gamedetails, new Comparator<SendData>() {
            @Override
            public int compare(SendData event1, SendData event2) {
                return event1.isnps38event.eventTime.compareTo(event2.isnps38event.eventTime);
            }
        });
    }
	

    public class MyTableModel extends AbstractTableModel  
    {  
        /**
		 * 
		 */
		private static final long serialVersionUID = 10086L;
		/* 
         * 这里和刚才一样，定义列名和每个数据的值 
         */  
        String[] columnNames =  
        { "序号", "联赛", "时间", "球队", "让球盘",  "PS38盘口水位",   "ISN盘口水位 ", "HG盘口水位 ", "大小盘",  "PS38大小盘水位",   "ISN大小盘水位 ", "HG大小盘水位 ","比分"};
        

  
        /** 
         * 构造方法，初始化二维数组data对应的数据 
         */  
        public MyTableModel()  
        {  

        }  
  
        // 以下为继承自AbstractTableModle的方法，可以自定义  
        /** 
         * 得到列名 
         */  
        @Override  
        public String getColumnName(int column)  
        {  
            return columnNames[column];  
        }  
          
        /** 
         * 重写方法，得到表格列数 
         */  
        @Override  
        public int getColumnCount()  
        {  
            return columnNames.length;  
        }  
  
        /** 
         * 得到表格行数 
         */  
        @Override  
        public int getRowCount()  
        {         
	        if(null == detailsData){
	    		return 0;
	    	}
            return detailsData.size();  
        }  
  
        /** 
         * 得到数据所对应对象 
         */  
        @Override  
        public Object getValueAt(int rowIndex, int columnIndex)  
        {  
            //return data[rowIndex][columnIndex];
        	return detailsData.elementAt(rowIndex)[columnIndex];
        }  
  
        /** 
         * 得到指定列的数据类型 
         */  
        @Override  
        public Class<?> getColumnClass(int columnIndex)  
        {  
        	
        	return String.class;
        	
//        	if(detailsData.size() == 0){
//        		return String.class;
//        	}
//
//            return detailsData.elementAt(0)[columnIndex].getClass();
        	
        }  
  
        /** 
         * 指定设置数据单元是否可编辑.这里设置"姓名","学号"不可编辑 
         */  
        @Override  
        public boolean isCellEditable(int rowIndex, int columnIndex)  
        {  
            return false;
        }  
          
        /** 
         * 如果数据单元为可编辑，则将编辑后的值替换原来的值 
         */  
        @Override  
        public void setValueAt(Object aValue, int rowIndex, int columnIndex)  
        {  
            detailsData.elementAt(rowIndex)[columnIndex] = (String)aValue;  
            /*通知监听器数据单元数据已经改变*/  
            fireTableCellUpdated(rowIndex, columnIndex);  
        }  
        
        public void updateTable(){
        	fireTableDataChanged();
        }
        
  
    }  
}
